package org.example.userToContentEnum;

/**
 * @FileName CounterEnum
 * @Description
 * @Author coderfun
 * @selfDesc coding in fun
 * @date 2024-11-02
 **/
public enum CounterEnum {
    LIKE("like", 1),
    COMMENT("comment", 2);

    private final String name;
    private final int code;

    CounterEnum(String name, int code) {
        this.name = name;
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public int getCode() {
        return code;
    }

    // 根据代码值查找对应的枚举常量
    public static CounterEnum fromCode(int code) {
        for (CounterEnum counter : CounterEnum.values()) {
            if (counter.getCode() == code) {
                return counter;
            }
        }
        throw new IllegalArgumentException("Unknown code: " + code);
    }

    // 根据名称查找对应的枚举常量
    public static CounterEnum fromName(String name) {
        for (CounterEnum counter : CounterEnum.values()) {
            if (counter.getName().equals(name)) {
                return counter;
            }
        }
        throw new IllegalArgumentException("Unknown name: " + name);
    }
}

